package com.weijing.pretty_app.dialog

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.weijing.pretty_app.R
import com.weijing.pretty_app.databinding.DialogImageBinding

/**
 * @param fangXiang "L" "R"
 * @param type 0-超限2000万次，1-手柄未连接，2-手柄水温报警，3-水流异常，4-水位异常
 */
class ImageDialog(private val fangXiang: String, private val type: Int) : DialogFragment() {

    private lateinit var binding: DialogImageBinding
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,
    ): View {
        binding = DialogImageBinding.inflate(inflater, container, false)
        //设置方向
        binding.dialogImageTvFangXiang.text = fangXiang

        when (type) {
            0 -> {    //超限2000万次
                binding.dialogImageIv1.setImageResource(R.mipmap.icon_handle_warning)
                binding.dialogImageTv1.setText(R.string.end_of_life)
            }

            1 -> {    //手柄未连接
                binding.dialogImageIv1.setImageResource(R.mipmap.icon_handle_unconnect)
                binding.dialogImageTv1.setText(R.string.Handle_not_connected)
            }

            2 -> {    //2-手柄水温报警
                binding.dialogImageIv1.setImageResource(R.mipmap.icon_handle_shuiwen_f7c)
                binding.dialogImageTv1.setText(R.string.Abnormal_water_temperature)
            }

            3 -> {//水流异常
                binding.dialogImageIv1.setImageResource(R.mipmap.icon_shui_liu_warning)
                binding.dialogImageTv1.setText(R.string.abnormal_water_flow)
            }

            4 -> {  //水位异常
                binding.dialogImageIv1.setImageResource(R.mipmap.icon_shui_wei_warning)
                binding.dialogImageTv1.setText(R.string.Abnormal_water_level)
            }
        }
        binding.dialogImageIvClose.setOnClickListener {
            dismiss()
        }

        dialog?.window?.setBackgroundDrawable(ColorDrawable(resources.getColor(android.R.color.transparent)))
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        isCancelable = true
        val dm = DisplayMetrics()
        this.activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        this.dialog?.window?.setLayout(dm.widthPixels, dm.heightPixels)
    }
}

open class ImageDialogShow {

    fun showing(context: FragmentManager, fangXiang: String, type: Int) {
        val signSuccessDialog = ImageDialog(fangXiang, type)
        if (!signSuccessDialog.isVisible && !signSuccessDialog.isAdded) {
            signSuccessDialog.show(context, "ImageDialog")
        }
    }

    fun closeDialog(context: FragmentManager) {
        val fragment = context.findFragmentByTag("ImageDialog")
        if (fragment != null) {
            val dialogFragment = fragment as DialogFragment
            dialogFragment.dismiss()
        }
    }
}